package com.gin.kotlin.no_gay_alliance.argument

import android.os.Parcelable
import kotlinx.parcelize.Parcelize
import okhttp3.HttpUrl.Companion.toHttpUrl
import java.net.URLDecoder

@Parcelize
data class UserCenterArgument(
    val userId: Long? = null,
    val username: String? = null,
    val html: Boolean = false,
) : Parcelable {

    companion object {
        fun parseUrl(url: String?): UserCenterArgument? {
            val httpUrl = url?.toHttpUrl() ?: return null
            // 用户名解码charset
            val charset = httpUrl.queryParameter("__inchst")?.takeIf { it.equals("UTF8", true) }?.let { "utf-8" } ?: "gbk"
            val userId = httpUrl.queryParameter("uid")?.toLongOrNull()
            val username = httpUrl.queryParameter("username")?.let { URLDecoder.decode(it, charset) }
            return UserCenterArgument(userId, username)
        }
    }
}